package com.lej.dataservice.club.bean.commercial

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type
import java.util.*


/**商业广告实体*/
@Entity
data class CommercialBean(
    @PrimaryKey
    val ad_id: String,
    val href: String?,
    /**0 http协议 1 自定义协议*/
    val href_type: String,
    val ext_pic: List<PicBean>,
    /**0图片 1视频*/
    val pic_type: Int,
    val timeout: Int,
    val title: String,
    val data_size: String,
    //上次展示的时间
    var showTime: Long? = null,
    val start_timestamp: Long,
    val end_timestamp: Long
)

data class PicBean(
    val height: String,
    val pic: String,
    val type: String,
    val width: String
) {
    fun ratio() = (width.toFloatOrNull() ?: 0f) / (height.toFloatOrNull() ?: 1f)
}

class PicBeanTypeConverters {
    var gson = Gson()

    @TypeConverter
    fun stringToPicList(data: String?): List<PicBean> {
        if (data == null) {
            return Collections.emptyList()
        }
        val listType: Type = object : TypeToken<List<PicBean>>() {}.type
        return gson.fromJson(data, listType)
    }

    @TypeConverter
    fun picListToString(someObjects: List<PicBean>): String {
        return gson.toJson(someObjects)
    }
}
